﻿import api from '../../api/api'
import {
  banner,
  notice,
  weixinopenlogin,
  homeGoods,
  productId,
  goodstocart,
  goodstocheckout, shoplist
} from '../../api/conf'
import Toast from 'tdesign-miniprogram/toast/index';

Page({
  data: {
    shopInfo: null,
    swiperConfig: {},
    goodsList: [],
    total: 0,
    noticeText: '',
    footBanner: {},
    isSpuSelectPopupShow: false,
    addCartGoodInfo: null,
    checkSkuIdArr: [],
    buyNum: 1,
    goodsId: 0,
    marquee2: {
      speed: 60,
      loop: -1,
      delay: 0,
    },
    showMakePhone: false
  },
  onLoad: function(options) {
    wx.showNavigationBarLoading()
    wx.showLoading({
        title: '加载中',
        mask: true
    })
    this.login()
    this.getTip()
    this.getBanner()
    this.getFootBanner()
    this.getShopInfo()
  },
  onShow: function (){
    this.getTabBar().init();
    this.getHotGoods()
  },

  login: function () {
    let token =  wx.getStorageSync('token');
    if (!token){
      wx.login({
        success(res) {
          api.get(weixinopenlogin, {
            code: res.code
          }).then(data => {
            if(data.Code === 200){
              wx.setStorageSync('guid', data.Data.guid)
              wx.setStorageSync('token', data.Data.token)
              wx.setStorageSync('openId', data.Data.openId)
            }
          })
        }
      })
    }
  },
  getBanner: function() {
    api.get(banner, {
      name: 'homehotbanner'
    }).then(res => {
      this.setData({
        swiperConfig: res.Data
      })
    })
  },
  getFootBanner: function () {
    api.get(banner, {
      name: 'footbanner'
    }).then(res => {
      this.setData({
        footBanner: res.Data
      })
    })
  },
  //获取公告
  getTip: function() {
    api.get(notice, {
      systemName: 'notice'
    }).then(res => {
      this.setData({
        noticeText: res.Data.Body
      })
    })
  },

  getHotGoods: function (){
    api.get(homeGoods, {}).then(res => {
      this.setData({
        goodsList: res.Data
      })
    })
  },

  goodListClickHandle: function (e) {
    let detailId = e.detail.goods.Id
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + detailId
    })
  },

  goodListAddCartHandle: function (e) {
    let goodsId = e.detail.goods.Id
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    wx.showNavigationBarLoading()

    api.get(productId, {
      id: goodsId
    }).then( res => {
      const addCartGoodInfo = res.Data
      this.setData({
        goodsId,
        addCartGoodInfo,
        isSpuSelectPopupShow: true,
        buyNum: 1
      })
      wx.hideLoading()
      wx.hideNavigationBarLoading()
    })
  },

  handleChooseItem: function ({ detail }) {
    this.setData({
      checkSkuIdArr: detail.checkSkuIdArr,
    })
  },

  handlePopupHide: function() {
    this.setData({
      isSpuSelectPopupShow: false,
    });
  },

  handleBuyNumChange: function (e) {
    const {value} = e.detail
    this.setData({
      buyNum: value
    })
  },

  handleAddCart: function () {
    const eventStatus = this.checkOutEvent()
    if(!eventStatus){
      return
    }
    let {buyNum} = this.data
    api.post(goodstocart, {
      PId: this.data.goodsId,
      Qty: buyNum,
      AttrValue: JSON.stringify(this.data.checkSkuIdArr)
    }).then( res => {
      this.setData({isSpuSelectPopupShow: false})
      Toast({
        context: this,
        selector: '#t-toast',
        message: '成功加入购物车',
      });
    })
  },

  gotoBuy: function () {
    const eventStatus = this.checkOutEvent()
    if(!eventStatus){
      return
    }
    api.post(goodstocheckout, {
      PId: this.data.goodsId,
      Qty: this.data.buyNum,
      AttrValue: JSON.stringify(this.data.checkSkuIdArr)
    }).then(res => {
      wx.navigateTo({
        url: '/pages/checkout/checkout',
      })
    })
  },

  checkOutEvent: function () {
    const {checkSkuIdArr} = this.data
    const isSpuSelectPopupShow = this.data.isSpuSelectPopupShow

    if(!isSpuSelectPopupShow){
      this.setData({isSpuSelectPopupShow: true })
      return false
    }

    if(checkSkuIdArr.length === 0 || checkSkuIdArr.includes(0)){
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请选择规格',
      });
      return false
    }
    return true
  },

  getShopInfo: function () {
    api.get(shoplist, {}).then( res => {
      const shopInfo = res.Data
      this.setData({shopInfo: shopInfo[0]})
      wx.setStorageSync('shopInfo', shopInfo[0])
      wx.hideLoading()
      wx.hideNavigationBarLoading()
    })
  },
  openMakePhone() {
    this.setData({ showMakePhone: true });
  },

  closeMakePhone() {
    this.setData({ showMakePhone: false });
  },

  onShareAppMessage: function() {
    return {
      title: '大螃蟹大螃蟹好吃的大螃蟹',
      path: '/pages/index/index'
    }
  },
})